package vic.controller;

import java.util.HashMap;
import java.util.List;

import vic.constdefinition.Config;
import vic.sendreceive.param.BaseParamDefinition;
import vic.sendreceive.param.PlayBoardParamDefinition;

import com.smartfoxserver.v2.entities.Room;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.smartfoxserver.v2.exceptions.SFSJoinRoomException;

public class LobbyRoom {
	Room room;
	public LobbyRoom(Room room){
		this.room = room;
	}
	public void joinLobby(User user){
		if(!room.containsUser(user))
			try {
				room.addUser(user);
				user.setProperty(BaseParamDefinition.UserStatus, Config.lobbyStatus);
			} catch (SFSJoinRoomException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	public void leaveLobby(User user){
		room.removeUser(user);
	}
	public List<User> getAllUser(){
		return room.getUserList();
	}
	public ISFSObject packAllUser(HashMap<Integer, Playboard> playboards){
		ISFSObject resObj = SFSObject.newInstance();
		String userList = "";
		for (User user : room.getUserList()) {
			userList += user.getId() + "_" + user.getName() + ";";
		}
		resObj.putUtfString(PlayBoardParamDefinition.userList, userList);
		if(playboards.size() > 0){
			String boardList = ""; 
			for(Playboard board : playboards.values()){
			   boardList += board.room.getId() + "_" + board.room.getName() + ";";
			}
			resObj.putUtfString(PlayBoardParamDefinition.boardList, boardList);
		}
		
		return resObj;
	}
	public boolean checkExistUser(User user){
		return this.getAllUser().contains(user);
	}
}
